import { _decorator, Asset, Component, EventKeyboard, Input, input, instantiate, KeyCode, loader, Node, Prefab, resources, Vec3 } from 'cc';
import { Flight } from './Flight';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {

    @property(Node)
    flight: Node;

    @property(Node)
    enemys: Node;

    @property(Node)
    bullet: Node;

    private flightComponent: Flight;

    protected onLoad(): void {
        this.flightComponent = this.flight.getComponent(Flight)
    }


    protected onEnable(): void {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    protected onDisable(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    start() {
        resources.load('Enemy01', (error: Error, data: Asset) => {
            if (error) {
                console.log('Enemy01 Error Exit...')
                return;
            }

            let enemy01Prefab = data as Prefab;

            let enemy01Node = instantiate(enemy01Prefab);

            enemy01Node.setPosition(
                -50, 150
            )

            this.enemys.addChild(enemy01Node)

        })

        resources.load('Enemy02', (error: Error, data: Asset) => {
            if (error) {
                console.log('Enemy02 Error Exit...')
                return;
            }

            let enemy02Prefab = data as Prefab;

            let enemy02Node = instantiate(enemy02Prefab);

            enemy02Node.setPosition(
                50, 150
            )

            this.enemys.addChild(enemy02Node)

        })

        this.startShoot();

    }

    private startShoot() {
        this.schedule(() => {
            resources.load(
                'Bullet01', (error: Error, data: Asset) => {
                    if (error) {
                        console.log('Bullet01 load error...')
                        return;
                    }

                    let bulletNode = instantiate(data as Prefab);


                    console.log('创建子弹')
                    this.bullet.addChild(bulletNode);

                    bulletNode.setWorldPosition(
                        this.flight.worldPosition
                    )

                }
            )
        }, 0.5)
    }

    update(deltaTime: number) {

    }

    onKeyDown(event: EventKeyboard) {

        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.flightComponent.speed.set(0, 5)
                break;
            case KeyCode.KEY_A:
                this.flightComponent.speed.set(-5, 0)
                break;
            case KeyCode.KEY_S:
                this.flightComponent.speed.set(0, -5)
                break;
            case KeyCode.KEY_D:
                this.flightComponent.speed.set(5, 0)
                break;
            default:
                break;
        }

    }

    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.flightComponent.speed.set(0, 0)
                break;
            case KeyCode.KEY_A:
                this.flightComponent.speed.set(0, 0)
                break;
            case KeyCode.KEY_S:
                this.flightComponent.speed.set(0, 0)
                break;
            case KeyCode.KEY_D:
                this.flightComponent.speed.set(0, 0)
                break;
            default:
                break;
        }
    }
}


